package com.cdks.wuxiu.api.component.cdks.kmsg;

import java.util.TreeMap;

import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.util.HttpUtils;
import com.cdks.wuxiu.api.util.TimeUtils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class PushMessage
{
    public static void init(String env)
    {
    }

    private static void sendMessage(long from, long[] to, int type, int receipt, Object msg, int offline, String umid)
    {
        TreeMap<String, Object> body  = new TreeMap<String, Object>();
        body.put("from", from);
        body.put("to", to);
        body.put("type", type);
        body.put("receipt", receipt);
        body.put("offline", offline);
        body.put("umid", umid);
        body.put("msg", msg);

        HttpUtils.send_post_request("http://localhost:8183/kmsg/sendMessage.php", null, JSONUtil.toJsonStr(body));
    }

    public static void sendChatMessage(long from, long to, int type, int receipt, Object msg, String umid)
    {
        sendMessage(from, new long[]{to}, type, receipt, msg, Constant.ENABLE_YES, umid);
    }
 
    public static void sendChatMessage(long from, long[] tos, int type, int receipt, Object msg)
    {
        String umid  = TimeUtils.getLocalDateTime("yyyyMMddHHmmssSSS");
        sendMessage(from, tos, type, receipt, msg, Constant.ENABLE_YES, umid);
    }

    public static void sendChatMessage(long from, long to, int type, int receipt, Object msg)
    {
        String umid  = TimeUtils.getLocalDateTime("yyyyMMddHHmmssSSS");
        sendMessage(from, new long[]{to}, type, receipt, msg, Constant.ENABLE_YES, umid);
    }
     
    public static void sendCallMessage(long from, long to, int type, int receipt, Object msg)
    {
        String umid  = TimeUtils.getLocalDateTime("yyyyMMddHHmmssSSS");
        sendMessage(from, new long[]{to}, type, receipt, msg, Constant.ENABLE_NO, umid);
    }
     
    public static void sendCallMessage(long from, long[] tos, int type, int receipt, Object msg)
    {
        String umid  = TimeUtils.getLocalDateTime("yyyyMMddHHmmssSSS");
        sendMessage(from, tos, type, receipt, msg, Constant.ENABLE_NO, umid);
    }

    public static int sendOfflineNotice(long userId, String title, String content) {
        sendOfflineNotice(new long[]{userId}, title, content, "default"); 

        return 0;
    }

    public static int sendOfflineNotice(long userId, String title, String content, String sound) {
        sendOfflineNotice(new long[]{userId}, title, content, sound); 

        return 0;
    }

    public static int sendOfflineNotice(long []userIds, String title, String content) {
        sendOfflineNotice(userIds, title, content, "default"); 

        return 0;
    }
    
    public static int sendOfflineNotice(long []userIds, String title, String content, String sound) {
        JSONObject json  = new JSONObject();
        json.set("userId", userIds);
        json.set("title", title);
        json.set("content", content);
        json.set("sound", sound);
        HttpUtils.send_post_request("http://localhost:8183/offlinePush/sendMessage.php", null, JSONUtil.toJsonStr(json)); 

        return 0;
    }
}
